package cn.com.chengmandian.bike.service.impl;

import cn.com.chengmandian.bike.constants.BikeConstants;
import cn.com.chengmandian.bike.mapper.EbOperationStationMapper;
import cn.com.chengmandian.bike.model.EbOperationStation;
import cn.com.chengmandian.bike.model.vo.OperationStationVo;
import cn.com.chengmandian.bike.service.IEbOperationStationService;
import cn.com.chengmandian.core.common.utils.LnglatUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 运营站点配置 服务实现类
 * </p>
 *
 * @author AutoGenerator
 * @since 2021-12-09
 */
@Service
public class EbOperationStationServiceImpl extends ServiceImpl<EbOperationStationMapper, EbOperationStation> implements IEbOperationStationService {
    @Autowired
    private EbOperationStationMapper operationStationMapper;

    @Override
    public List<OperationStationVo> getStationList(Integer areaId) {
        return operationStationMapper.getStationList(areaId);
    }

    @Override
    public List<OperationStationVo> getNearStationList(Integer areaId, BigDecimal lng, BigDecimal lat) {
        BigDecimal minLat = lat.subtract(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal minLng = lng.subtract(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal maxLat = lat.add(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        BigDecimal maxLng = lng.add(BikeConstants.NEAR_BIKE_RANG).setScale(6, BigDecimal.ROUND_HALF_UP);
        return operationStationMapper.getNearStation(areaId, minLat, maxLat, minLng, maxLng);
    }

    @Override
    public OperationStationVo getStation(Integer id) {
        return operationStationMapper.getStation(id);
    }

    @Override
    public OperationStationVo checkIn(Integer areaId, BigDecimal lng, BigDecimal lat) {
        List<OperationStationVo> list = operationStationMapper.getStationList(areaId);
        for(OperationStationVo item : list) {
            if (LnglatUtil.isInPolygon(lng.doubleValue(), lat.doubleValue(), item.getLngs().split(","), item.getLats().split(","))) {
                return item;
            }
        }
        return null;
    }
}
